﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using mojoPortal.Web.RS.PRM.AppCode.Lib;

namespace mojoPortal.Web.RS.PRM.Controls
{
    public partial class AddPropertyControl : RSBaseUserControl
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            string prevurl = string.Empty;
            if (Session["PreviousURL"] != null)
                prevurl = Session["PreviousURL"].ToString();
            else
                prevurl = Request.UrlReferrer.ToString();
            Response.Redirect(prevurl);
        }

        public void LoadPropertyData(int companyid, int propertyid)
        {
            RSPRMDataLookupLoader.LoadTypeDDL(companyid, "PROPERTY_CAT", ddlPropertyType);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            int PropertyID = 0;
            float BedNum = txtBedNum.Text == "" ? 0 : float.Parse(txtBedNum.Text);
            float BathNum = txtBathNum.Text == "" ? 0 : float.Parse(txtBathNum.Text);
            int SqFt = txtSqFt.Text == "" ? 0 : int.Parse(txtSqFt.Text);
            int YearBuilt = txtYearBuilt.Text == "" ? 0 : int.Parse(txtYearBuilt.Text);
            int AppraisedValue = txtAppraisedValue.Text == "" ? 0 : int.Parse(txtAppraisedValue.Text);
            int AssessedValue = txtAssessedValue.Text == "" ? 0 : int.Parse(txtAssessedValue.Text);
            int PropertyTypeID = int.Parse(ddlPropertyType.SelectedValue);
            int OwnerID = 0;
            string Street = txtStreet.Text;
            string City = txtCity.Text;
            string State = txtState.Text;
            string Zip = txtZip.Text;
            string County = txtCounty.Text;
            string Country = txtCountry.Text;
            string SubdivisionName = txtSubdivisionName.Text;
            string Description = txtDescription.Text;
            bool IsOccupied = false;
            string ElementarySchool = txtElementarySchool.Text;
            string MiddleSchool = txtMiddleSchool.Text;
            string HighSchool = txtHighSchool.Text;

            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite)
            {
                SqlCommand sqlCmd = new SqlCommand();

                SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@BedNum", SqlDbType.Float, 0, ParameterDirection.Input, BedNum);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@BathNum", SqlDbType.Float, 0, ParameterDirection.Input, BathNum);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@SqFt", SqlDbType.Int, 0, ParameterDirection.Input, SqFt);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@YearBuilt", SqlDbType.Int, 0, ParameterDirection.Input, YearBuilt);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@AppraisedValue", SqlDbType.Int, 0, ParameterDirection.Input, AppraisedValue);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@AssessedValue", SqlDbType.Int, 0, ParameterDirection.Input, AssessedValue);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@Street", SqlDbType.NVarChar, 250, ParameterDirection.Input, Street);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@City", SqlDbType.NVarChar, 50, ParameterDirection.Input, City);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@State", SqlDbType.NVarChar, 50, ParameterDirection.Input, State);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@County", SqlDbType.NVarChar, 50, ParameterDirection.Input, County);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@Country", SqlDbType.NVarChar, 50, ParameterDirection.Input, Country);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@Zip", SqlDbType.NVarChar, 10, ParameterDirection.Input, Zip);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@SubdivisionName", SqlDbType.NVarChar, 250, ParameterDirection.Input, SubdivisionName);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@Description", SqlDbType.NVarChar, 250, ParameterDirection.Input, Description);

                SQLHelper.AddParamToSQLCmd(sqlCmd, "@ElementarySchool", SqlDbType.NVarChar, 250, ParameterDirection.Input, ElementarySchool);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@MiddleSchool", SqlDbType.NVarChar, 250, ParameterDirection.Input, MiddleSchool);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@HighSchool", SqlDbType.NVarChar, 250, ParameterDirection.Input, HighSchool);

                SQLHelper.AddParamToSQLCmd(sqlCmd, "@ManagerID", SqlDbType.Int, 0, ParameterDirection.Input, userIdentity.RSEmployeeId);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@OwnerID", SqlDbType.Int, 0, ParameterDirection.Input, OwnerID);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@PropertyTypeID", SqlDbType.Int, 0, ParameterDirection.Input, PropertyTypeID);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@PropertyID", SqlDbType.Int, 0, ParameterDirection.Input, PropertyID);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@IsOccupied", SqlDbType.Bit, 0, ParameterDirection.Input, IsOccupied);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@CompanyId", SqlDbType.Int, 0, ParameterDirection.Input, userIdentity.RSCompanyId);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@CurrentUser", SqlDbType.NVarChar, 25, ParameterDirection.Input, userIdentity.RSCurrentUser);
                SQLHelper.SetCommandType(sqlCmd, CommandType.StoredProcedure, "rs_uspSaveProperty");
                SQLHelper.ExecuteScalarCmd(sqlCmd);

                int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
                if (returnValue > 0)
                {
                    if (FileUpload1.HasFile)
                    {
                        string folder = userIdentity.RSPhotoPath;
                        string fileLocalPath = Server.MapPath(folder);
                        string photoname = FileUpload1.FileName.Replace(" ", "_");
                        string photopath = folder + returnValue + "_" + photoname;
                        string filename_localpath = fileLocalPath + returnValue + "_" + photoname;
                        FileUpload1.SaveAs(filename_localpath);

                        PropertyID = returnValue;
                        string SQL = "UPDATE rs_tblProperty SET PhotoPath=@PhotoPath WHERE PropertyID=@PropertyID AND CompanyID=@CompanyId";
                        sqlCmd = new SqlCommand();
                        SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                        SQLHelper.AddParamToSQLCmd(sqlCmd, "@PhotoPath", SqlDbType.NVarChar, 150, ParameterDirection.Input, photopath);
                        SQLHelper.AddParamToSQLCmd(sqlCmd, "@PropertyID", SqlDbType.Int, 0, ParameterDirection.Input, PropertyID);
                        SQLHelper.AddParamToSQLCmd(sqlCmd, "@CompanyId", SqlDbType.Int, 0, ParameterDirection.Input, userIdentity.RSCompanyId);
                        SQLHelper.SetCommandType(sqlCmd, CommandType.Text, SQL);
                        SQLHelper.ExecuteScalarCmd(sqlCmd);
                    }

                    string prevurl = string.Empty;
                    if (Session["PreviousURL"] != null)
                        prevurl = Session["PreviousURL"].ToString();
                    else
                        prevurl = Request.UrlReferrer.ToString();
                    ucModalActionMessageControl.ShowModal("Property Successfully Saved!", prevurl, true);
                    btnSave.Enabled = false;
                }
            }
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);

        }
    }
}